home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / JDBC / JDBC_TES / JDBCTEST / EXAMPLE / GETSTRIN.JAV < prev    next >
Encoding:
Text File  |  1996-10-18  |  4.5 KB  |  136 lines

  1. /*
  2.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "LICENSE"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  *
  17.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  18.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  19.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  20.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  21.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  22.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  23.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  24.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  25.  * HIGH RISK ACTIVITIES.
  26.  */
  27.  
  28.  
  29. package jdbcTest.example;
  30.  
  31. import java.sql.*;
  32. import jdbcTest.harness.*;
  33.  
  34. // A test class must extend jdbcTest.harness.TestModule
  35. public class getString extends jdbcTest.harness.TestModule  {
  36.  
  37.     public  void run () {
  38.  
  39.     TestCase test;
  40.     Connection con = null;
  41.     java.sql.CallableStatement stmt = null;
  42.     ResultSet rs = null;
  43.     DatabaseMetaData meta = null;
  44.     
  45.     try {
  46.  
  47.         //Creates and executes a test case object used for
  48.         //controlling this test case.
  49.         test = createTestCase("Test example.getString");
  50.         execTestCase(test);
  51.  
  52.         // The getUrl, getSignon, and getPassword methods provide
  53.         // the info entered for the test run to the test.
  54.         con = DriverManager.getConnection (getUrl(), getSignon(), getPassword());
  55.  
  56.         meta = con.getMetaData();
  57.         if (!meta.supportsStoredProcedures()) {
  58.         result("Does not support Stored Procedures");
  59.         }
  60.         else {
  61.  
  62.         stmt = con.prepareCall( "{call JDBC_GET_STRING(?)}" );
  63.         stmt.registerOutParameter(1, java.sql.Types.CHAR);
  64.         stmt.executeUpdate();
  65.  
  66.         //The test method registers the object being tested
  67.         //and logs the method string. This is used for logging
  68.         //what the test is attempting to do and with what
  69.         //object.
  70.         test(stmt, "getString()");
  71.         String s = stmt.getString(1);
  72.  
  73.         //The result method logs the value of the test results.
  74.         result(s.length());
  75.         result(s);
  76.  
  77.         //If the expression is true, assert logs the fact this
  78.         //assertion passed. If it is false, assert logs the
  79.         //failure an aborts the test. There is a companion
  80.         //method named verify which does the same except that
  81.         //instead aborting it notes the failure and
  82.         //continues. At the end a call to passed is converted
  83.         //to a call to failed if any verify has failed.
  84.         assert(s.equals("bc        "), "The CHAR value must be 'bc        '");
  85.         // The getSupportedSQLType method returns a full type
  86.         // description for the driver data type that
  87.         // corresponds to SQL type. This is just data
  88.         // retreived from DatabaseMetadata.getTypeInfo. A null
  89.         // ref indicates there is no support for this
  90.         // type. See SupportedSQLType.java in this directory
  91.         // for the details.
  92.         if (getSupportedSQLType(Types.VARCHAR) == null) {
  93.             result("Does not support VARCHAR SQL type");
  94.         } else {
  95.             stmt = con.prepareCall( "{call JDBC_GET_VSTRING(?)}" );
  96.             stmt.registerOutParameter(1, java.sql.Types.VARCHAR);
  97.             stmt.executeUpdate();
  98.  
  99.             test(stmt, "getString()");
  100.             s = stmt.getString(1);
  101.  
  102.             result(s.length());
  103.             result(s);
  104.  
  105.             assert(s.equals("bc"), "The VARCHAR value must be 'bc'");
  106.         }
  107.         }
  108.  
  109.         //Since the test reached here it is assumed to have passed
  110.         //unless a verify detected a failure.
  111.         passed();
  112.     }
  113.     catch (Exception ex) {
  114.  
  115.         // The exception method handles logging all
  116.         // exceptions. Since the test is executed in its own
  117.         // thread it need to do its own exception handling.
  118.         exception(ex);
  119.     }
  120.     finally {
  121.         try{
  122.         if (con != null) con.close();
  123.         }
  124.         catch (SQLException ex) {
  125.         }
  126.  
  127.         // The stop method terminates the test's thread. This must
  128.         // be called or the test harness will not know the test
  129.         // has completed.
  130.         stop();
  131.     }
  132.  
  133.     }
  134.  
  135. }
  136.